package com.unei.groovy.batch

import com.alibaba.fastjson.TypeReference
import com.google.gson.reflect.TypeToken
import com.unei.java.BookUnitSectionDto
import org.unei.common.helper.GsonHelper
import org.unei.common.helper.JsonHelper

/**
 * Created by sunlei on 2015/1/16.
 */
def json = new File("D:\\mygit\\demo\\groovy\\src\\main\\resources\\json.txt").text

List<BookUnitSectionDto> list = JsonHelper.toGenericBean(json, new TypeReference<List<BookUnitSectionDto>>() {});

println list.size()
//list.each{println(it.getUnitList().size())}

//List<BookUnitSectionDto> gsonList=GsonHelper.toBeanList(json);
//gsonList.each {
//    def BookUnitSectionDto book=it;
//    List<Unit> unitList=book.getUnitList();
//    if(unitList)
//        println(unitList.size())
//}

//compare fastjson to gson
class BUS {
    String busName
    List<Unit> unitList = []
}

class Unit {
    String unitName
    List<Section> sectionList = []
}

class Section {
    String sectionName
    List<Step> stepList = []
}

class Step {
    String stepName
    List<Group> groupList = []
}

class Group {
    String name;
}

println "compare fastjson to gson"

Group g = new Group()
g.name = "group"

Step step = new Step()
step.stepName = "step"
step.getGroupList().add(g)

Section section = new Section()
section.sectionName = "section"
section.getStepList().add(step)

Unit unit = new Unit()
unit.unitName = "unit"
unit.getSectionList().add(section)

BUS bus = new BUS()
bus.busName = "bus"
bus.getUnitList().add(unit)

List busList = [bus]

String busStr = JsonHelper.toJsonStr(busList)
List objList = JsonHelper.toGenericBean(busStr, new TypeReference<List<BUS>>() {})

List objList2 = GsonHelper.fromJson(busStr, new TypeToken<List<BUS>>() {}.getType())

println("over...")